<?php

namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\marquee\MarqueeModel;
use bc\util\backend\Page;
use bc\model\marquee\BlockModel;
use bc\util\Uploader;
use bc\model\marquee\StatisticsModel;

class MarqueeController extends BackendController
{
	
	public function actionIndex()
	{
		$model = new MarqueeModel();
		
		$blockModel = new BlockModel();
		$params = '1 = 1';
		
                if($this->post('block_id')){
                    $block_id = $this->post('block_id');
                }else{
                    $block_id = $this->get('block_id');
                }
		               
		if($block_id)
		{
			$params .= ' AND bc_marquee.block_id = ' . $block_id;
		}
		
		$page = $this->get('page', 1);
		$model->sort = 'id DESC';
                $params .=' AND bc_marquee.status = 1';
		$list = $model->getBackendList($params, $page, self::PAGE_SIZE);
		
		$blocks = $blockModel->getList(['block_status' => 1]);
		
		$total = $model->getBackendTotal($params);
		
		$pagination = new Page($total, self::PAGE_SIZE);
		
		$view['list'] = $list;
		$view['blocks'] =  $blocks;
		$view['pagination'] = $pagination;
		$view['block_id'] = $block_id;
		
		return $this->render('index', $view);
	}
	
	/**
	 * 
	 */
	public function actionAdd()
	{
		
		$blockModel = new BlockModel();
		$blockList = $blockModel->getList('1 = 1');
		

		$view['block_list'] = $blockList;
			
		return $this->render('add', $view);
	}
	
	
	/**
	 * 
	 */
	public function actionEdit()
	{
		$id = $this->get('id');
		if(!$id)
			$this->redirect('/');
	
		$model = new MarqueeModel();
		$row = $model->getRowById($id);

		$block_id = $row['block_id'];
		$blockModel = new BlockModel();
		$block = $blockModel->getRowById($block_id);
	
		if(!$row)
			$this->redirect('/');
			
//		$statisticsModel = new StatisticsModel();
//		
//		$today = date('Y-m-d');
//		$params = ' marquee_id = ' . $id . ' AND create_time >= ' . $today;
//		$statistics = $statisticsModel->getList($params);
		
			
		$view['row'] = $row;
		$view['block'] = $block;
		//$view['statistics'] = $statistics[0];
		return $this->render('edit', $view);
	}
	
	
	/**
	 * 保存
	 */
	public function actionSave()
	{
		if($this->getRequest()->getMethod() === 'POST' )
		{
			$model = new MarqueeModel();
			$id = $this->post('id', 0);	
			
			$data['block_id'] = $this->post('block_id');
			$data['description'] = $this->post('description', '');
			$data['link'] = $this->post('link', '');
			$data['status'] = 1;
			
				
			//Banner上传
			if($_FILES['image'])
			{
				$up = new Uploader( 'image' , $this->getUploadConfig() );
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['image'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
					
			}
				
			if($id)
			{         
                                $data['update_time'] = date('Y-m-d H:i:s');
				$model->modify($data, $id);
			}
			else
			{
				$data['sort'] = 10000;
				$data['create_time'] = date('Y-m-d H:i:s');
				$model->add($data);
			}
			$output['msg'] = '操作成功!';
				
		}
		else
		{
			$output['msg'] = '非法请求!';
		}
		
		$output['url'] = '/marquee/index?block_id='.$data['block_id'];
		$this->forward($output);
	}
        public function actionDelete()
    {
                       $id = $this->get('id');
                       $model = new MarqueeModel();
                       $data['status'] = 0;
                       $model ->modify($data,$id);
                       $list = $model->getRowById($id);

                       $output['msg'] = '操作成功!';
                       $output['url'] = '/marquee/index?block_id='.$list['block_id'];
                       $this->forward($output);
               
    }
	
	
	/**
	 * 
	 */
	public function actionSort()
	{
		$block_id = $this->get('block_id');
		$model = new MarqueeModel();
		$model->sort = 'status ASC, create_time DESC';
		$list = $model->getList(['block_id' => $block_id, 'status' => 1]);
		
		$blockModel = new BlockModel();
		$block = $blockModel->getRowById($block_id);
		
		$view['list'] = $list;
		$view['block'] = $block;
		return $this->render('sort', $view);
	}
	
	
	/**
	 * 
	 */
	public function actionSortsave()
	{
		$output['msg'] = '操作成功!';
		
		$marqueeIds = $_POST['marquee'];
		$i = 1;
		if($marqueeIds)
		{
			$model = new MarqueeModel();
			foreach ($marqueeIds as $id)
			{
				$object = new \stdClass();
				$object->sort = $i;
				$object->id = $id;
				$data[] = $object;
				$i++;
			}
			$rs = $model->setSort($data);
			if($rs)
			{
				$output['msg'] = '操作成功!';
			}
			else 
			{
				$output['msg'] = '操作失败!';
			}
		}
		
		
		$output['url'] = '/marquee';
		$this->forward($output);
	}
	
	/**
	 * 重新统计数据
	 */
	public function actionReload()
	{
		$marquee_id = $this->post('marquee_id');
		$model = new MarqueeModel();
		
		$row = $model->getRowById($marquee_id);
		
		$statisticsModel = new StatisticsModel();
		//获取最后数据
		$rs =  $statisticsModel->tractionUpdate($marquee_id, $row['view_count']);
		if($rs)
		{
			$msg['result'] = 1;
			$msg['message'] = '执行成功';
		}
		else 
		{
			$msg['result'] = 0;
			$msg['message'] = '执行失败';
		}
		
		echo json_encode($msg);
		exit;
		
	}
        
	
	/**
	 * @return array
	 */
	private function getUploadConfig()
	{
		//上传文件目录
		$path = UPLOAD_PATH . 'upload/marquee/';
		//上传配置
		$config = array(
				"savePath" => $path ,             //存储文件夹
				"maxSize" => 100000 ,                   //允许的文件最大尺寸，单位KB
				"allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" )  //允许的文件格式
		);
			
		//背景保存在临时目录中
		$config[ "savePath" ] = $path;
		return $config;
	}
}

